#include "server.h"

int initThreadPool(threadPool_t *pThreadPool, int workerNum) {
    pThreadPool->workerNum = workerNum;
    pThreadPool->workerArr = (pthread_t *) calloc(workerNum, sizeof(pthread_t));
    initTaskQueue(&pThreadPool->taskQueue);
    pThreadPool->epollFd = -1;
    memset(&(pThreadPool->pathBuffer[0]), 0, 256 * 256);
    memset(&(pThreadPool->userBuffer[0]), 0, 128 * 128);
    strcpy(pThreadPool->home, "/home/liu/myCloud/");
    pthread_mutex_init(&pThreadPool->mutex, NULL);
    pthread_cond_init(&pThreadPool->cond, NULL);
    return 0;
}

int makeWorkers(threadPool_t *pThreadPool) {
    for (int i = 0; i < pThreadPool->workerNum; ++i) {
        pthread_create(&pThreadPool->workerArr[i], NULL, threadFunc, pThreadPool);
    }
    return 0;
}

int initTaskQueue(taskQueue_t *p) {
    memset(p, 0, sizeof(taskQueue_t));
    return 0;
}

int enQueue(taskQueue_t *pTaskQueue, int netFd) {
    node_t *pNewNode = (node_t *) calloc(1, sizeof(node_t));
    pNewNode->netFd = netFd;
    if (!pTaskQueue->queueSize) {
        pTaskQueue->front = pNewNode;
        pTaskQueue->rear = pNewNode;
    } else {
        pTaskQueue->rear->next = pNewNode;
        pTaskQueue->rear = pNewNode;
    }
    (pTaskQueue->queueSize)++;
    return 0;
}

int deQueue(taskQueue_t *pTaskQueue) {
    node_t *curr = pTaskQueue->front;
    pTaskQueue->front = pTaskQueue->front->next;
    free(curr);
    if (!pTaskQueue->front) {
        pTaskQueue->rear = NULL;
    }
    (pTaskQueue->queueSize)--;
    return 0;
}
